<?php
/**
 * Created by PhpStorm.
 * User: Shroom Ari
 * Date: 11/20/14
 * Time: 11:24 AM
 */
class Mproduct extends MY_Model{
    public $table = 'product';
    protected $_gallery_path = "";
    protected $_gallery_url = "";
    public function __construct(){
        parent::__construct();
        //Lấy đường dẫn url của thư mục chứa hình ảnh được upload
        $this->_gallery_url = base_url()."public/images/";
        //Lấy đường dẫn vật lý của thư mục chứa hình ảnh đươc upload
        $this->_gallery_path = realpath(APPPATH. "../public/images");
    }

    public function do_upload(){
        $config = array('upload_path'   => $this->_gallery_path,
            'allowed_types' => 'gif|jpg|png',
            'max_size'      => '2000');
        $this->load->library("upload",$config);
        if(!$this->upload->do_upload("urlImage")){
            $error = array($this->upload->display_errors());
        }else{
            $image_data = $this->upload->data();
            return $image_data['file_name'];
        }
        //kết thúc công đoạn upload hình ảnh
    }
    function get_product($perpage,$offset,$catId,$query){
        $this->db->like('product_name', $query);
        $this->db->where('product_category_id',$catId);
        $query = $this->db->get('product',$perpage,$offset);
        return $query->result_array();
    }
    function  getProduct(){
        $this->db->select('*');
        $this->db->from('product');
        $query = $this->db->get();
        return $query->result_array();

    }
    function get_product_by_category($id){
        $query = $this->db->query("select tb1.*, new_price from product tb1 left join (select product.product_id, round((1- promotion.discount)*product.unit_price,2)
        as new_price from promotion_detail, promotion, product where promotion.promotion_id in (select promotion_id from promotion where start_date <= now()
        and end_date>= now()) and promotion.promotion_id = promotion_detail.promotion_id and promotion_detail.product_id = product.product_id) tb2 on tb1.product_id = tb2.product_id
        where product_category_id = $id");
        return $query->result_array();
    }
    function get_productbyid($id){
        $query = $this->db->query("select tb1.*, new_price from product tb1 left join (select product.product_id, round((1- promotion.discount)*product.unit_price,2)
        as new_price from promotion_detail, promotion, product where promotion.promotion_id in (select promotion_id from promotion where start_date <= now() and end_date>= now())
        and promotion.promotion_id = promotion_detail.promotion_id and promotion_detail.product_id = product.product_id) tb2 on tb1.product_id = tb2.product_id where tb1.product_id = $id");
        return $query->result_array();
    }
    function count_all($catId,$query){
        $query = $this->db->query("select count(*) as count from product where product_category_id = $catId and product_name like '%$query%'");
        return $query->result()[0]->count;
    }
    function delete($id){
        $this->db->where('product_id',$id);
        $this->db->delete('product');
    }
    function updateProduct($id,$dt){
        $this->db->where('product_id',$id);
        if ($this->db->update('product',$dt))
            return TRUE;
        else return FALSE;
    }

    function search($q){
        $query = $this->db->query("select tb1.*, new_price from product tb1 left join (select product.product_id, round((1- promotion.discount)*product.unit_price,2)
        as new_price from promotion_detail, promotion, product where promotion.promotion_id in (select promotion_id from promotion where start_date <= now()
        and end_date>= now()) and promotion.promotion_id = promotion_detail.promotion_id and promotion_detail.product_id = product.product_id) tb2 on tb1.product_id = tb2.product_id
        where tb1.product_name like '%$q%'");
        return $query->result_array();
    }

}